package learnjava.basic.other;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TestSocket {
    public static void main(String[] args) throws Exception{

        new Server1().start();
//        new Client1().start();
        Socket socket = new Socket("localhost",2021);
        OutputStream outputStream = socket.getOutputStream();
        System.out.println(outputStream);//java.net.SocketOutputStream@6d6f6e28
        outputStream.close();
        System.out.println(socket);//Socket[addr=localhost/127.0.0.1,port=2021,localport=57245]
        outputStream = socket.getOutputStream();//Exception in thread "main" java.net.SocketException: Socket is closed
        System.out.println(outputStream);//outputStream可能是单例模式，关了以后就没了
    }
}

class Client1 extends Thread{
    @Override
    public void run() {
        try {
            Socket socket = new Socket("localhost",2021);
            //给服务器发一张图片
            OutputStream os = socket.getOutputStream();
            FileInputStream fis = new FileInputStream("E:\\wallpaper\\yoyo.png");
            byte[] buffer = new byte[256];
            int len = 0;
            while ((len=fis.read(buffer))!=-1){
                os.write(buffer,0,len);
            }
            System.out.println("图片传完了");
            Thread.sleep(2000);
            socket.shutdownOutput();
            System.out.println("告知服务器图片传完了");
            //接收服务器返回的消息
            InputStream is = socket.getInputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while ((len=is.read(buffer))!=-1){
                baos.write(buffer,0,len);
            }
            System.out.println(baos.toString());
            //关流
            fis.close();
            os.close();
            is.close();
            baos.close();
            socket.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

class Server1 extends Thread{
    @Override
    public void run() {
        try{
            ServerSocket ss = new ServerSocket(2021);
            Socket socket = ss.accept();
            //接收客户端发来的图片
            InputStream is = socket.getInputStream();
            FileOutputStream fos = new FileOutputStream("E:\\wallpaper\\yoyo1.png");
            byte[] buffer = new byte[256];
            int len = 0;
            //is是阻塞IO，如果客户端不告知何时结束接收，它就会一直等待
            while ((len=is.read(buffer))!=-1){
                fos.write(buffer,0,len);
            }
            //1,客户端：图片传完了
            //2,客户端：告知服务器图片传完了
            //3,服务器：客户端说图片传完了
            //4,服务器：文件已收到
            //由2,3可见，只有客户端终止了传输，服务器端才知道不再等待了
            System.out.println("客户端说图片传完了");


            //通知客户端文件已收到
            OutputStream os = socket.getOutputStream();
            os.write("文件已收到".getBytes());
            //关闭流
            is.close();
            fos.close();
            os.close();
            socket.close();
            ss.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}


